package com.situ.company.project.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.company.project.model.ProjectModel;
import com.situ.company.project.service.IProjectService;
import com.situ.company.project.service.impl.ProjectServiceImpl;
import com.situ.company.score.model.ScoreModel;
import com.situ.company.util.FmtRequest;

@WebServlet("/ProjectServlet")
public class ProjectServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	private IProjectService service = new ProjectServiceImpl();
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Object res=null;
		switch(req.getParameter("action")) {
			case "add":
				res=add(req);
				break;
			case "page":
				res=page(req);
				break;
			case "del":
				res=del(req);
				break;
			case "get":
				res=get(req);
				break;
			case "upd":
				res=upd(req);
				break;
			case "list":
				res=list(req);
				break;
		}
		FmtRequest.write(resp.getWriter(),res);
		
	}

	private Object list(HttpServletRequest req) {
		return service.selectList(FmtRequest.parseModel(req, ProjectModel.class));
	}

	private Object upd(HttpServletRequest req) {
		return service.update(FmtRequest.parseModel(req, ProjectModel.class));
	}

	private Object get(HttpServletRequest req) {
		return service.selectModel(FmtRequest.parseModel(req, ProjectModel.class));
	}

	private Object del(HttpServletRequest req) {
		return service.delete(FmtRequest.parseModel(req,ProjectModel.class));
	}

	private Object page(HttpServletRequest req) {
		String code=req.getParameter("code");
		String name=req.getParameter("name");
		String pageIndex=req.getParameter("pageIndex");
		String pageLimit=req.getParameter("pageLimit");
		ProjectModel model=new ProjectModel();
		model.setCode(code);
		model.setName(name);
		model.setPageIndex(Integer.parseInt(pageIndex));
		model.setPageLimit(Integer.parseInt(pageLimit));
		model.setPageOn(true);
		List<ProjectModel> list=service.selectList(model);
		Integer count = service.selectCount(model);
		Map<String,Object> map=new HashMap<>();
		map.put("list", list);
		map.put("count", count);
		
		return map;
	}

	private Object add(HttpServletRequest req) {
		return service.insert(FmtRequest.parseModel(req, ProjectModel.class));
	}

}
